from django.shortcuts import render
from .models import Person, Address, Author, book
from django.db.models import Q, Max, Min, Sum, Count
from rest_framework.parsers import JSONParser
from rest_framework.decorators import api_view
import base64  # 加密模块
from django.http import HttpResponse, JsonResponse
import logging
# 鉴权模块
import random, string
from django.core.signing import Signer
from django.core.cache import cache
from datetime import timedelta
#
from django.db.models import Q


# Create your views here.
@api_view(['POST'])
def add_person(request):
    data = JSONParser().parse(request)
    name = data.get('name')
    age = data.get('age')
    gender = data.get('gender')
    Person(name=name, age=age, gender=gender).save()
    return JsonResponse({
        "code": 200,
        "msg": "添加成功"
    })


@api_view(['POST'])
def add_Address(request):
    data = JSONParser().parse(request)
    personid = data.get('personid')
    city = data.get('city')
    town = data.get('town')
    village = data.get('village')
    detailaddress = data.get('detailaddress')
    Address(city=city, town=town, village=village, detailaddress=detailaddress, person_id=personid).save()
    return JsonResponse({
        "code": 200,
        "msg": "添加成功"
    })


@api_view(['POST'])
def add_author(request):
    data = JSONParser().parse(request)
    name = data.get('name')
    age = data.get('age')
    gender = data.get('gender')
    Author(name=name, age=age, gender=gender).save()
    return JsonResponse({
        "code": 200,
        "msg": "作者添加成功"
    })


@api_view(['POST'])
def add_book(request):
    data = JSONParser().parse(request)
    name = data.get('name')
    price = data.get('price')
    publisher = data.get('publisher')
    # author_id = data.get('author_id')
    author_name = data.get('author_name')
    authorobj = Author.objects.get(name=author_name)
    # book(name=name, price=price, publisher=publisher, author_id=author_id).save()
    book(name=name, price=price, publisher=publisher, author=authorobj).save()
    return JsonResponse({
        "code": 200,
        "msg": "书籍添加成功"
    })


@api_view(['GET'])
def get_info(request):
    name = request.GET.get('author_name')
    authorobj = Author.objects.get(name=name)
    bookobj = authorobj.book.all()
    logging.info(f'看看作者关联出来的所有对象：{bookobj}')
    lis = []
    for item in bookobj:
        lis.append({
            '作者': authorobj.name,
            '年龄': authorobj.age,
            '性别': authorobj.gender,
            '书名': item.name,
            '价格': item.price,
            '出版社': item.publisher,
        })
    return JsonResponse({
        'code': 200,
        'msg': '查询作者书籍成功',
        'data': lis
    })


@api_view(['GET'])
def bookselectauthor(request):
    name = request.GET.get('name')
    # bookobj = book.objects.get(name=name) # 获取一个对象
    bookobj = book.objects.filter(name=name).first()  # 获取一个列表
    return JsonResponse({
        'code': 200,
        'bookname': name,
        '作者姓名': bookobj.author.name,
        '作者年龄': bookobj.author.gender,
        '作者性别': bookobj.author.age,
    })


@api_view(['GET'])
def getgroupbyinfo(request):
    datas = book.objects.values('publisher').annotate(maxprice=Max('price'), minprice=Min('price'),
                                                      count=Count('price'), totalprice=Max('price'))
    logging.info(f'查看分组后的数据：{datas}')
    lis = []
    for item in datas:
        lis.append({
            '出版社': item.get('publisher'),
            '最贵书的价格': '%.2f' % item.get('maxprice'),
            '最便宜书的价格': '%.2f' % item.get('minprice'),
            '书籍数量': item.get('count'),
        })
    return JsonResponse({
        'code': 200,
        'msg': '请求成功',
        'data': lis,
    })
